<template>
    <div class="page-account">
        <div v-if="showI18n" class="page-account-header">
            <i-header-i18n />
        </div>
        <div class="page-account-container">
            <div class="page-account-top">
                <div class="page-account-top-logo">
                    <img src="@/assets/images/logoBAM.png" alt="logo">
                </div>
                <div class="page-account-top-desc" style="display:none">快速实现任何 Web、App 和企业软件的身份认证和用户管理</div>
            </div>
            <Login @on-submit="handleSubmit">
                <UserName name="username" value="admin" />
                <Password name="password" value="123456" enter-to-submit />
                <div class="page-account-auto-login">
                    <Checkbox v-model="autoLogin" size="large">{{ $t('page.login.remember') }}</Checkbox>
                    <a href="">{{ $t('page.login.forgot') }}</a>
                </div>
                <Submit :loading="isBtnLoading">{{ $t('page.login.submit') }}</Submit>
            </Login>
            <div class="page-account-other" style="display:none">
                <span>{{ $t('page.login.other') }}</span>
                <img src="@/assets/svg/icon-social-wechat.svg" alt="wechat">
                <img src="@/assets/svg/icon-social-qq.svg" alt="qq">
                <img src="@/assets/svg/icon-social-weibo.svg" alt="weibo">
                <router-link class="page-account-register" :to="{ name: 'register' }">{{ $t('page.login.signup') }}</router-link>
            </div>
        </div>
        <i-copyright />
    </div>
</template>
<script>
    import iCopyright from '@/components/copyright';
    import { mapActions } from 'vuex';
    import mixins from '../mixins';
    import {UserLogin} from '@/api/user.js'
    import { getHeaderName, getMenuSider, getSiderSubmenu } from '@/libs/system';
    import { frameInRoutes } from '@/router/routes';

    export default {
        mixins: [ mixins ],
        components: { iCopyright },
        data () {
            return {
                autoLogin: true,
                isBtnLoading:false
            }
        },
        methods: {
            ...mapActions('admin/account', [
                'login'
            ]),
            ...mapActions('admin/user', [
                'set'
            ]),
            /**
             * @description 登录
             * 表单校验已有 iView Pro 自动完成，如有需要修改，请阅读 iView Pro 文档
             */
            handleSubmit (valid, values) {
                if (valid && this.isBtnLoading==false) {
                    this.isBtnLoading=true;
                    const { username, password } = values;
                    UserLogin(
                        {
                            account: username,
                            password: password
                        }
                    ).then((result) => {
                        this.isBtnLoading=false;
                        if(result.errcode==0){
                            const data=result.data;
                            //设置用户名、头像 
                            this.set({avatar:require("@/assets/images/userDft.jpg"),name:data.Name});
                            //登录成功跳转址
                            const toPath=this.$route.query.redirect || '/';
                            this.$router.replace(toPath);
                        }
                        else{
                            this.$Message.error(result.msg);
                        }
                    }).catch(error=>{
                        this.isBtnLoading=false;
                    });
                }
            }
        }
    };
</script>
